#!/bin/bash

scripts_root="/home/pi/siweather/scripts"

maestro_dir="/home/pi/siweather/maestro"

connected=`$maestro_dir/./UscCmd --list 2>&1 | grep Maestro | awk '{ print $1}'`
if [ $connected -lt 1 ]; then echo "No Maestro servo controller found!"; exit 1; fi

CAM=`lsusb | grep Canon`

if [ -z "$CAM" ]; then
        echo "Camera already off"
	exit 5
fi

LIM=20
COUNT=0

while [ -n "$CAM" -a $COUNT -lt $LIM ]; do
	COUNT=`expr $COUNT + 1`
	$maestro_dir/./UscCmd --servo 3,8000 &> /dev/null
	sleep 1
	$maestro_dir/./UscCmd --servo 3,0000 &> /dev/null
	sleep 4
	CAM=`lsusb | grep Canon`
	CAM=`lsusb | grep Canon`
	
done

if [ $COUNT -lt $LIM ]; then
	echo "Camera turned off after "$COUNT" seconds."
	exit 0
else 
	echo "Failed to turn off the camera."
	exit 1
fi

